package com.mxpio.erp.technology.controller;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.technology.entity.ProdTecRoutAudit;
import com.mxpio.erp.common.technology.entity.ProdTecRoutProcAudit;
import com.mxpio.erp.common.technology.service.ProdTecRoutAuditService;
import com.mxpio.erp.common.technology.service.ProdTecRoutProcAuditService;
import com.mxpio.erp.common.technology.vo.AuditOpinionVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProdTecRoutAuditController", description = "产品工艺路线审核接口")
@RestController
@RequestMapping("/erp/tech/prodrout/audit/")
public class ProdTecRoutAuditController {

    @Autowired
    private ProdTecRoutAuditService routAuditService;

    @Autowired
    private ProdTecRoutProcAuditService routProcAuditService;

    @GetMapping("listByItem/{routId}")
    @Operation(summary = "产品工艺路线审核", description = "获取产品工艺路线审核（无分页）", method = "GET")
    public Result<ProdTecRoutAudit> listByItem(@Parameter(description="routId") @PathVariable(name = "routId", required = true) String routId){
        ProdTecRoutAudit bomInfo = routAuditService.listRoutAudit(routId);
        return Result.OK(bomInfo);
    }
    
    @GetMapping("page")
    @Operation(summary = "工艺路线审核", description = "获取工艺路线审核（分页）", method = "GET")
    public Result<Page<ProdTecRoutAudit>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProdTecRoutAudit> bomInfos = routAuditService.listPage(ProdTecRoutAudit.class,page,criteria);
        return Result.OK(bomInfos);
    }



    @PutMapping("audit/{routId}")
    @Operation(summary = "审核工艺路线", description = "审核工艺路线", method = "PUT")
    public Result<ProdTecRoutAudit> audit(@Parameter(description="routId") @PathVariable(name = "routId", required = true) String routId,
                                      @RequestBody AuditOpinionVO auditOpinionVO) {
        ProdTecRoutAudit routAudit = routAuditService.getById(ProdTecRoutAudit.class,routId);
        if(routAudit.getUseType().equals(OrderStatusEnums.BomStatus.AUDITING.getStatusCode())){
            ProdTecRoutAudit audit = routAuditService.audit(routAudit,auditOpinionVO);
            return Result.OK(audit);
        }else {
            return Result.error("非审核中状态不可审核");
        }
    }


}
